<?php
namespace app\shell;

use share\console\CConsole;
use share\console\CLIRunner;
use share\service\AmqpService;
use PhpAmqpLib\Wire\AMQPTable;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class CAmqp extends CLIRunner{

    /**
     * 需要用到的交换机 队列 绑定关系  一次声明
     */
    public function define_action(){
        
        $config = AmqpService::get_config();
        $queueName = $config['queue'];
        $exchange = $config['exchange'];
        $exchangeDelay = $config['exchange_delay'];
        // 普通交换机
        AmqpService::instance()->set_exchange_option([
            'passive' => false,
        ])->bind_queue_and_exchange($queueName,$exchange,"");

        // 延迟交换机
        AmqpService::instance()->set_exchange_option([
            'type' => "x-delayed-message",
            'passive' => false,
            'arguments' => new AMQPTable(array(
                    'x-delayed-type' => 'direct',
                )
            ),            
        ])->bind_queue_and_exchange($queueName,$exchangeDelay,"");
    }


    public function get_queue_num_action(){
        $config = AmqpService::get_config();
        $queueName = $config['queue'];
        $options = AmqpService::defalut_queue_options([
            'passive' => true,
        ]);
        $res = AmqpService::instance()->declare_queue($queueName,$options);
        // 正常返回格式 [队列名, 消息数,  消费者数]
        // ["common_queue",3,0]
        return $res;
    }

    public function set_queue_info_action(){
        AmqpService::instance()->set_exchange_option([

            ])->set_queue_option([
                
        ])->bind_queue_and_exchange("test_queue","test_ex","test_router");
    }

    public function publish_action(){
        $data = [
            'service' => 'app\\service\\DeviceBaseService@async',
            'params' => [
                10000
            ]
        ];
        AmqpService::instance()->simplePush($data);
    } 
    
    public function publish_delay_action(){
        $data = [
            'service' => 'app\\service\\DeviceBaseService@async',
            'params' => [
                 10000
            ]
        ];
        AmqpService::instance()->simplePushDelay($data,10);    
    } 

    public function connect_test_action(){

        $appKey = "31394556"; //在控制台自有App，单击密钥对应的查看，可显示App的App Key和App Secret 创建App时系统
        $appSecret = "78b6387253f52d0d91ccc07171d571dc";     
        $random = time();
        //建议使用机器UUID、MAC地址、IP等唯一标识等作为clientId。便于您区分识别不同的客户端。
        $clientId = "php_connect_test_50";

        $userName = $clientId . "|authMode=appkey"                 
                . ",signMethod=" . "SHA256"
                . ",random=" . $random
                . ",appKey=" . $appKey
                . ",groupId=" . $appKey . "|";
        $signContent = "random=" . $random;
        $password = hash_hmac('sha256', $signContent, $appSecret);

        $config = [
            'host' => 'ilop.iot-amqp.cn-shanghai.aliyuncs.com',
            'port' => 5671,
            'username' => $userName,
            'password' => $password ,
        ];
        $connect = new AMQPStreamConnection($config['host'],$config['port'],$config['username'],$config['password']);
        return $connect;

    }
}